package com.cg.controller.business;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.BusinessSearch;
import com.cg.entity.UploadImg;
import com.cg.service.impl.BusinessSearchServiceImpl;
import com.cg.utils.Constant;
import com.cg.utils.DateUtils;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.UploadUtil;
import com.cg.utils.annotation.SysLog;
import com.google.gson.Gson;

@RestController
@RequestMapping("/sys/businesssearch")
public class BusinessSearchController extends AbstractController{
	
	@Autowired
	BusinessSearchServiceImpl businessSearchServiceImpl;
	
	@Autowired
	private UploadUtil uploadUtil;
	/**访问地址*/
	@Value("${global.upload.path}")
	private String path;
	/**存放地址*/
	@Value("${global.upload.docBase}")
	private String docBase;
	
	private String mypath="appmenu/";
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:businesssearch:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		
		//查询列表数据
		Query query = new Query(params);
//		
//		BusinessSearch search=new BusinessSearch();
		List<BusinessSearch> businessList = businessSearchServiceImpl.queryList(query);
		int total = businessSearchServiceImpl.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", businessList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:businesssearch:info")
	public R info(@PathVariable("id") Long id){
		BusinessSearch search = businessSearchServiceImpl.queryObject(id);
		
		
		return R.ok().put("search", search);
	}
	
	/**
	 * 上传图片
	 */
	@SysLog("上传")
	@RequestMapping("/upload")
	@RequiresPermissions("sys:businesssearch:save")
	public R upload(HttpServletRequest request){
		
		Gson gson = new Gson();
		String filePath = uploadUtil.uploadImg(request,mypath);
		UploadImg uploadImg =new UploadImg();
		uploadImg.setSrc(filePath);
		uploadImg.setTitle("123");
		R  result = R.ok();
//		result.put("msg", "成功");
		result.put("data",uploadImg);
		return  result;
	}
	
	
	/**
	 * 保存用户
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("sys:businesssearch:save")
	public R save(@RequestBody BusinessSearch search){
		search.setCreateTime(DateUtils.dateToDateString(new Date()));
		businessSearchServiceImpl.save(search);
		
		return R.ok();
	}
	/**
	 * 修改用户
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("sys:businesssearch:update")
	public R update(@RequestBody BusinessSearch search){
		
//		comment.set(getUserId());
		businessSearchServiceImpl.update(search);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:businesssearch:delete")
	public R delete(@RequestBody Long[] ids){
		
		businessSearchServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
	
}
